package com.cameron.orderdemo;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;

public class OrderDemo {

	public static final String AUTHORITY = "com.cameron.orderdemo";
	public static final Uri AUTHORITY_URI = Uri.parse("content://" + AUTHORITY);
	
	public static final Uri CONTENT_URI = Uri.withAppendedPath(AUTHORITY_URI, "orderdemo");
	
	//public static final String ID = "_id";
	//public static final String NUMBER = "number";
	//public static final String NAME = "cache_name";
	public static final String ID = "_id";
	public static final String MARKET_NAME = "market_name";
	public static final String STORE_NAME = "store_name";
	public static final String RECORD_DATE_TIME = "record_date_time";
	public static final String GOODS_TYPE = "goods_type";
	public static final String GOODS_NAME = "goods_name";
	public static final String MEARSURE_UNIT = "measure_unit";
	public static final String GOODS_COLOR = "goods_color";
	public static final String GOODS_SIZE = "goods_size";
	public static final String GOODS_PRICE = "goods_price";
	public static final String GOODS_AMOUNT = "goods_amount";
	public static final String DELIVERY_DATE = "delivery_date";
	public static final String GOODS_PHOTO = "goods_photo";
	public static final String REMARKS = "remarks";
	
	private static final int MAX_LENGTH = 11;
	/*
	public class GoodsInfo {
		public String market_name;
		public String store_name;
		public String record_date;
		public String goods_name;
		public String measure_unit;
		public String goods_color;
		public String goods_size;
		public String goods_price;
		public String goods_amount;
		public String delivery_date;
		public String goods_photo;
		public String remarks;
	};*/
	
	public static boolean add(Context context, GoodsInfo goodsInfo, boolean insert) {
		if (TextUtils.isEmpty(goodsInfo.goods_name) ||
			TextUtils.isEmpty(goodsInfo.goods_type) ||
			TextUtils.isEmpty(goodsInfo.goods_amount) ||
			TextUtils.isEmpty(goodsInfo.goods_price)) {
			return false;
		}
		
		ContentResolver cr = context.getContentResolver();
		ContentValues values = new ContentValues();
		values.put(MARKET_NAME, goodsInfo.market_name);
		values.put(STORE_NAME, goodsInfo.store_name);
		values.put(RECORD_DATE_TIME, goodsInfo.record_date_time);
		values.put(GOODS_TYPE, goodsInfo.goods_type);
		values.put(GOODS_NAME, goodsInfo.goods_name);
		values.put(MEARSURE_UNIT, goodsInfo.measure_unit);
		values.put(GOODS_COLOR, goodsInfo.goods_color);
		values.put(GOODS_SIZE, goodsInfo.goods_size);
		values.put(GOODS_PRICE, goodsInfo.goods_price);
		values.put(GOODS_AMOUNT, goodsInfo.goods_amount);
		values.put(DELIVERY_DATE, goodsInfo.delivery_date);
		values.put(GOODS_PHOTO, goodsInfo.goods_photo);
		values.put(REMARKS, goodsInfo.remarks);
		if (insert) {
			values.put(ID, goodsInfo._id);
		    cr.insert(CONTENT_URI, values);
		} else {
			cr.update(CONTENT_URI, values, ID + "=" + goodsInfo._id, null);
		}
		
		return true;
	}
	
	public static void remove(Context context, GoodsInfo goodsInfo) {
		if (TextUtils.isEmpty(goodsInfo.goods_name)) {
			return;
		}
		
		Uri url = Uri.withAppendedPath(OrderDemo.CONTENT_URI, String.valueOf(goodsInfo._id));
		context.getContentResolver().delete(url, null, null);
	}
	
	public static boolean contains(Context context, GoodsInfo goodsInfo) {
		if (TextUtils.isEmpty(goodsInfo.goods_name)) {
			return false;
		}
		
		ContentResolver cr = context.getContentResolver();
		Cursor c = cr.query(CONTENT_URI, null, GOODS_NAME + "=? AND" + MARKET_NAME + "=? AND" + STORE_NAME + "=?", 
				new String[] {goodsInfo.goods_name, goodsInfo.store_name, goodsInfo.store_name}, null);
		try {
			if (c != null && c.moveToFirst()) {
				return true;
			}
		} finally {
			if (c != null) {
				c.close();
			}
		}
		
		return false;
	}
	/*
	public static String formatNumber(String number) {
		number = number.replaceAll("-", "");
		
		int length = number.length();
		
		if (length <= MAX_LENGTH) {
			return number;
		}
		
		return number.substring(length - MAX_LENGTH);
	}*/
}
